package com.usian.controller.user;

import com.usian.pojo.error.Error;
import com.usian.pojo.shop.Shop;
import com.usian.service.ErrorService;
import com.usian.service.ShopService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private ErrorService errorService;

    @Resource
    private ShopService shopService;

    //商品状态 state 0-新增/修改 1-启用  3-提交审核/待审核  5-审核通过  7-上架  9-下架  11-审核失败
    /**
     * 启用    -1
     * */
    @RequestMapping("/toEnable")
    public String toEnable(Integer sid){
        //得到sid对应的shop
        Shop shop = shopService.getById(sid);
        shop.setState(1);
        //修改
        shopService.updateById(shop);
        //重定向到展示页面
        return "redirect:/shop/findAll";
    }

    /**
     * 提交审核    -3
     * */
    @RequestMapping("/submitForReview")
    public String submitForReview(Integer sid){
        //得到sid对应的shop
        Shop shop = shopService.getById(sid);
        shop.setState(3);
        //修改
        shopService.updateById(shop);
        //重定向到展示页面
        return "redirect:/shop/findAll";
    }

    /**
     * 审核通过    -5
     *      审核通过标准：
     *              不能和 已上架(state=7) 的商品名称重复
     *              审核时提交的商品库存要小于100
     * */
    @RequestMapping("/Approved")
    public String Approved(Integer sid, Model model){
        //得到sid对应的shop
        Shop shop = shopService.getById(sid);

        //循环遍历shop表 判断是否通过标准，不通过则调用 审核失败方法 errorReason-失败原因
        /*List<Shop> shopList = shopService.list(new QueryWrapper<>());
        for (Shop s : shopList) {
            if (s.getState()==7 && s.getSname().equals(shop.getSname())){
                String errorReason = "和已上架的商品名称重复";
                this.auditFailed(sid,errorReason);
                return "redirect:/shop/findAll";
            }else if (shop.getStock()<100){
                String errorReason = "库存小于100";
                this.auditFailed(sid,errorReason);
                return "redirect:/shop/findAll";
            }
        }*/

        //根据得到的shop对应的sname和stock去查询
        Integer number = shopService.compareSname(shop);
        if (shop.getStock()<100){
            String errorReason = "库存小于100";
            this.auditFailed(sid,errorReason);
            return "redirect:/shop/findAll";
        } else if (number>0){
            String errorReason = "和已上架的商品名称重复";
            this.auditFailed(sid,errorReason);
            return "redirect:/shop/findAll";
        }

        shop.setState(5);
        //修改
        shopService.updateById(shop);
        //重定向到展示页面
        return "redirect:/shop/findAll";
    }



    /**
     * 审核失败    -11
     * */
    @RequestMapping("/auditFailed")
    public String auditFailed(Integer sid,String errorReason){
        //得到sid对应的shop
        Shop shop = shopService.getById(sid);
        shop.setState(11);
        //修改
        shopService.updateById(shop);

        //审核失败要维护失败原因
        Error error = new Error(sid,errorReason,new Date());
        errorService.save(error);

        //重定向到展示页面
        return "redirect:/shop/findAll";
    }

    /**
     * 上架   -7
     * */
    @RequestMapping("/toGrounding")
    public String toGrounding(Integer sid){
        //得到sid对应的shop
        Shop shop = shopService.getById(sid);
        shop.setState(7);
        //修改
        shopService.updateById(shop);
        //重定向到展示页面
        return "redirect:/shop/findAll";
    }

    /**
     * 下架   -9
     * */
    @RequestMapping("/offTheShelf")
    public String offTheShelf(Integer sid){
        //得到sid对应的shop
        Shop shop = shopService.getById(sid);
        shop.setState(9);
        //修改
        shopService.updateById(shop);
        //重定向到展示页面
        return "redirect:/shop/findAll";
    }

}
